﻿using System;
using System.Collections.Generic;
using System.Linq;

// 订单明细类
class OrderDetail
{
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }

    public OrderDetail(string productName, double price, int quantity)
    {
        ProductName = productName;
        Price = price;
        Quantity = quantity;
    }

    public override string ToString()
    {
        return $"商品名称：{ProductName}，单价：{Price}，数量：{Quantity}";
    }
}

// 订单类
class Order
{
    public int OrderId { get; set; }
    public string Customer { get; set; }
    public List<OrderDetail> Details { get; set; }

    public double TotalAmount => Details.Sum(detail => detail.Price * detail.Quantity);

    public Order(int orderId, string customer, List<OrderDetail> details)
    {
        OrderId = orderId;
        Customer = customer;
        Details = details;
    }

    public override bool Equals(object obj)
    {
        var order = obj as Order;
        return order != null && OrderId == order.OrderId;
    }

    public override string ToString()
    {
        return $"订单号：{OrderId}，客户：{Customer}，订单总金额：{TotalAmount}";
    }
}

// 订单服务类
class OrderService
{
    private List<Order> orders = new List<Order>();

    public void AddOrder(Order order)
    {
        if (orders.Contains(order))
        {
            throw new InvalidOperationException("订单已存在，无法重复添加！");
        }
        orders.Add(order);
    }

    public void RemoveOrder(int orderId)
    {
        Order orderToRemove = orders.FirstOrDefault(order => order.OrderId == orderId);
        if (orderToRemove == null)
        {
            throw new InvalidOperationException("订单不存在，无法删除！");
        }
        orders.Remove(orderToRemove);
    }

    public List<Order> QueryOrders(Func<Order, bool> predicate)
    {
        return orders.Where(predicate).OrderBy(order => order.TotalAmount).ToList();
    }

    public void SortOrders(Comparison<Order> comparison)
    {
        orders.Sort(comparison);
    }
}

class Program
{
    static void Main(string[] args)
    {
        OrderService orderService = new OrderService();

        while (true)
        {
            Console.WriteLine("请选择操作：");
            Console.WriteLine("1. 添加订单");
            Console.WriteLine("2. 删除订单");
            Console.WriteLine("3. 查询订单");
            Console.WriteLine("4. 退出");
            Console.Write("请输入操作编号：");
            string choice = Console.ReadLine();

            switch (choice)
            {
                case "1":
                    AddOrder(orderService);
                    break;
                case "2":
                    RemoveOrder(orderService);
                    break;
                case "3":
                    QueryOrders(orderService);
                    break;
                case "4":
                    return;
                default:
                    Console.WriteLine("无效操作，请重新输入！");
                    break;
            }
        }
    }

    static void AddOrder(OrderService orderService)
    {
        Console.Write("请输入订单号：");
        int orderId = int.Parse(Console.ReadLine());
        Console.Write("请输入客户：");
        string customer = Console.ReadLine();
        List<OrderDetail> details = new List<OrderDetail>();
        while (true)
        {
            Console.Write("请输入商品名称：");
            string productName = Console.ReadLine();
            Console.Write("请输入单价：");
            double price = double.Parse(Console.ReadLine());
            Console.Write("请输入数量：");
            int quantity = int.Parse(Console.ReadLine());
            details.Add(new OrderDetail(productName, price, quantity));
            Console.Write("是否继续添加订单明细？(Y/N)：");
            string continueInput = Console.ReadLine();
            if (continueInput.ToUpper() != "Y")
            {
                break;
            }
        }
        Order order = new Order(orderId, customer, details);
        orderService.AddOrder(order);
        Console.WriteLine("订单添加成功！");
    }

    static void RemoveOrder(OrderService orderService)
    {
        Console.Write("请输入要删除的订单号：");
        int orderId = int.Parse(Console.ReadLine());
        try
        {
            orderService.RemoveOrder(orderId);
            Console.WriteLine("订单删除成功！");
        }
        catch (InvalidOperationException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    static void QueryOrders(OrderService orderService)
    {
        Console.WriteLine("请选择查询方式：");
        Console.WriteLine("1. 按订单号查询");
        Console.WriteLine("2. 按商品名称查询");
        Console.WriteLine("3. 按客户查询");
        Console.WriteLine("4. 按订单金额查询");
        Console.Write("请输入查询方式编号：");
        string choice = Console.ReadLine();
        Func<Order, bool> predicate = null;
        switch (choice)
        {
            case "1":
                Console.Write("请输入订单号：");
                int orderId = int.Parse(Console.ReadLine());
                predicate = order => order.OrderId == orderId;
                break;
            case "2":
                Console.Write("请输入商品名称：");
                string productName = Console.ReadLine();
                predicate = order => order.Details.Any(detail => detail.ProductName == productName);
                break;
            case "3":
                Console.Write("请输入客户：");
                string customer = Console.ReadLine();
                predicate = order => order.Customer == customer;
                break;
            case "4":
                Console.Write("请输入订单金额下限：");
                double minAmount = double.Parse(Console.ReadLine());
                predicate = order => order.TotalAmount >= minAmount;
                break;
            default:
                Console.WriteLine("无效查询方式，请重新输入！");
                return;
        }
        List<Order> queryResult = orderService.QueryOrders(predicate);
        foreach (var order in queryResult)
        {
            Console.WriteLine(order);
        }
    }
}
